package my_web.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import Proxy.DynamicProxy;
import ReqBean.RequestBean;
import jsonDate.JsonDateValueProcessor;
import my_web.Interface.DeliverMissionInterface;
import my_web.bean.Emp;
import my_web.model.DeliverMissionDto;
import my_web.page.Page;
import my_web.service.DeliverMissionservice;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
@WebServlet({"/DeliverMissionToList.do","/feedbackMission.do","/MissionFeedback.do","/checkMission.do","/MissionCheck.do","/abandonMission.do"})
public class DeliverMissionServlet  extends HttpServlet{
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		res.setContentType("text/html;charset=utf-8");
		String path=req.getServletPath();
		HttpSession session=req.getSession();
		DeliverMissionInterface service=null;
		try {
			service=DynamicProxy.creatServiceProxy(DeliverMissionInterface.class, DeliverMissionservice.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if("/DeliverMissionToList.do".equals(path)){//员工查看自己的任务清单
			try {
				res.setContentType("application/json;charset=UTF-8");//设置媒体格式
				Emp emp=(Emp)session.getAttribute("Emp");//获取当前登陆用户
				DeliverMissionDto dto=RequestBean.getBeanFromRequest(DeliverMissionDto.class,req);//页面请求参数dto
				System.out.println("dto="+dto);
				Page pg=(Page)service.DynamicQueryAllDeliverMissions(dto,emp);//显示满足条件的任务记录
				JsonConfig jsonConfig = new JsonConfig();
				jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
				String json=JSONObject.fromObject(pg.getEasyPage(),jsonConfig).toString();
				PrintWriter out = res.getWriter();
				out.print(json);
				System.out.println("json="+json);
				out.flush();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else if("/feedbackMission.do".equals(path)){//去汇报
			res.setContentType("application/json;charset=UTF-8");
			String m_id = req.getParameter("m_id");
			DeliverMissionDto dto= service.feedbackMission(m_id);//获取需要汇报的任务
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
			String json = JSONObject.fromObject(dto,jsonConfig).toString();
			System.out.println("json="+json);
			PrintWriter out = res.getWriter();
			out.print(json);
			out.flush();
			out.close();		
		}else if("/MissionFeedback.do".equals(path)){//确认汇报
			try {
				res.setContentType("application/json;charset=UTF-8");
				PrintWriter out = res.getWriter();
				Emp emp=(Emp)session.getAttribute("Emp");
				DeliverMissionDto dto=RequestBean.getBeanFromRequest(DeliverMissionDto.class, req);//任务dto
				dto.setE_id(emp.getE_id());//执行人id
				service.getFeedback(dto);//任务状态改为 已汇报 
				out.print("{\"msg\":\"反馈成功\"}");
				out.flush();
				out.close();	
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else if("/checkMission.do".equals(path)){//去任务审核
			String m_id = req.getParameter("m_id");
			DeliverMissionDto dto= service.checkMission(m_id);//获取需要审核的任务
			res.setContentType("application/json;charset=UTF-8");
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
			String json = JSONObject.fromObject(dto,jsonConfig).toString();
			PrintWriter out = res.getWriter();
			out.print(json);
			System.out.println("json="+json);
			out.flush();
			out.close();
		}else if("/MissionCheck.do".equals(path)){//确认审核
			try {
				res.setContentType("application/json;charset=UTF-8");
				DeliverMissionDto dto=RequestBean.getBeanFromRequest(DeliverMissionDto.class, req);
				PrintWriter out = res.getWriter();
				Emp emp=(Emp)session.getAttribute("Emp");
				dto.setE_id(emp.getE_id());
				service.getMissionCheck(dto);//任务状态 已审核
				out.print("{\"msg\":\"审核成功\"}");
				out.flush();
				out.close();	
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else if("/abandonMission.do".equals(path)){
			res.setContentType("application/json;charset=UTF-8");
			String m_id = req.getParameter("m_id");
			PrintWriter out  = res.getWriter();
			service.getAbandonMission(m_id);
			out.print("{\"msg\":\"任务失败\"}");
			out.flush();
			out.close();
		}
	}
	protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		this.doGet(req, res);
	}
}
